<?php
class ImageParser implements IParserEngine {
	private     $logger;
	function __construct(){
         $this->logger = LoggerManager::getLogger('core.include.ResultParser.ImageParser');
	}
	public function parse($uploadedFile, $fullFileName, $thumbnailFileName, $ext, $thumbnailWidth, $thumbnailHeight,  $proportion = true){
		$files['full']        =   $fullFileName . "." . $ext;
        $files['thumbnail']   =   $thumbnailFileName . "." . $ext;
		$fullFileName         =   APP_ROOT . $fullFileName . "." . $ext;
		$thumbnailFileName    =   APP_ROOT . $thumbnailFileName . "." . $ext;
		$this->logger->info("Start function parse. Params: $fullFileName, $thumbnailFileName, $ext, $thumbnailWidth, $thumbnailHeight, $proportion");
        clearstatcache();
		$this->logger->debug("Creating thumbnail resource {" . $uploadedFile['tmp_name'] .", $fileType, $thumbnailWidth, $thumbnailHeight, $proportion}");
        $res        =   $this->createThumbnail($uploadedFile['tmp_name'], $uploadedFile['type'], $thumbnailWidth, $thumbnailHeight, $proportion);

        // Copy uploaded file to the given location
        $this->logger->debug("Copying uploaded file from tmp folder to given location {fullFileName='$fullFileName'}");
        move_uploaded_file($uploadedFile['tmp_name'], $fullFileName);


        // Create thumbnail file
        $this->logger->debug("Creating thumbnail file  {thumbnailName='$fullFileName'}");
        $res['function']($res['resource'], $thumbnailFileName);

        $this->logger->info("Finish function parse. Params: $fullFileName, $thumbnailFileName, $ext, $thumbnailWidth, $thumbnailHeight, $proportion");

        // Destroy all resource
        imagedestroy($res['resource']);

        return $files;
	}

	private function createThumbnail($fileName, $fileType, $thumbnailWidth, $thumbnailHeight, $proportion) {
		switch ($fileType){
            case "image/jpeg":
            case "image/pjpeg":
                $function  =   'imagecreatefromjpeg';
                $creatingFunction =    'imagejpeg';
                break;
            case "image/gif":
                $function  =   'imagecreatefromgif';
                $creatingFunction =    'imagegif';
                break;
            case "image/png":
            case "image/x-png":
            default:
                $function  =   'imagecreatefrompng';
                $creatingFunction =    'imagepng';
                break;
        }
        $this->logger->debug("Creating thumbnail image {fileType=$fileType, createImageResourceFunction=$function, createImageFileFunction=$creatingFunction}");
        $im = $function($fileName);
        $x = imagesx($im);
        $y = imagesy($im);
        if($x <= $thumbnailWidth && $y <= $thumbnailHeight) {
            $thumbnailResoure   =   $im;
        }
        else {
            if (!$proportion) {
                if($x >= $y) {
                    $newx = $thumbnailWidth;
                    $newy = $newx * $y / $x;
                } else {
                    $newy = $thumbnailHeight;
                    $newx = $x / $y * $newy;
                }
            }
            else {
                $ratio  =   min(array($thumbnailWidth/$x,$thumbnailHeight/$y));
                $newx   =   $ratio*$x;
                $newy   =   $ratio*$y;
            }
            $thumbnailResoure = imagecreatetruecolor($newx, $newy);
            imagecopyresized($thumbnailResoure, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
        }
        imagedestroy($im);
        $result =   array(
            'function'  =>   $creatingFunction,
            'resource'  =>   $thumbnailResoure
        );

        return $result;
	}
}
?>